package service

import (
	"qld2devservice/models"
	"qld2devservice/protocol"
	"qld2devservice/utils"
	"strconv"
)

func DeviceOpen(userId int64, snNum, orderSn string, amount, balance, openType, timeType, hour int64) (timeOutFlag bool, openFlag int64, openMsg string, err error) {
	qldOutlets, err := models.GetQldOutletsBySnNum(snNum)
	if err != nil {
		newErr := utils.WrapError(err, "DeviceOpen:1,获取设备信息失败，"+snNum)
		return false, 0, "", newErr
	} else if len(qldOutlets) == 0 {
		newErr := utils.NewError(utils.ErrorTypeStop, "DeviceOpen:获取设备失败，"+snNum)
		return false, 0, "", newErr
	} else {
		hostUrl, err := getProtocolHostUrl(qldOutlets[0].ProtocolId)
		if err != nil {
			newErr := utils.WrapError(err, "DeviceOpen:2,获取设备协议服务路径失败，"+snNum)
			return false, 0, "", newErr
		} else {
			//fmt.Println("获取的地址:", hostUrl)
			if qldOutlets[0].ProtocolId == 10 || qldOutlets[0].ProtocolId == 8 || qldOutlets[0].ProtocolId == 11 {
				return protocol.QiCheGuoBiaoOpen(hostUrl, userId, snNum, orderSn, amount, balance)
			} else if qldOutlets[0].ProtocolId == 9 || qldOutlets[0].ProtocolId == 12 {
				return protocol.ShengHongOpen(hostUrl, userId, snNum, orderSn, amount, balance)
			} else if qldOutlets[0].ProtocolId == 13 {
				return protocol.YuanYingOpen(hostUrl, userId, snNum, orderSn, balance, openType, timeType, hour)
			} else if qldOutlets[0].ProtocolId == 16 {
				return protocol.PonovoSocketOpen(hostUrl, userId, snNum, orderSn, balance, openType, timeType, hour)
			} else if qldOutlets[0].ProtocolId == 17 {
				return protocol.LvccProtocolOpen(hostUrl, userId, snNum, orderSn, balance, openType, timeType, hour)
			} else {
				return protocol.QiCheGuoBiaoOpen(hostUrl, userId, snNum, orderSn, amount, balance)
				//return false, 0, "", nil
			}
		}
	}
}
func DeviceClose(userId int64, snNum string) (timeOutFlag bool, closeFlag int64, amount int64, electricity float32, duration int64, orderSn, recordAmount, closeMsg string, err error) {
	qldOutlets, err := models.GetQldOutletsBySnNum(snNum)
	if err != nil {
		newErr := utils.WrapError(err, "DeviceClose:获取设备信息失败，"+snNum)
		return false, 0, 0, 0, 0, "", "", "", newErr
	} else if len(qldOutlets) == 0 {
		newErr := utils.NewError(utils.ErrorTypeStop, "DeviceClose:获取设备失败，"+snNum)
		utils.MyPrint(utils.PRINT_ERROR, newErr.Error())
		return false, 0, 0, 0, 0, "", "", "", newErr
	} else {
		hostUrl, err := getProtocolHostUrl(qldOutlets[0].ProtocolId)
		if err != nil {
			newErr := utils.WrapError(err, "DeviceClose:2,获取设备协议服务路径失败，"+snNum)
			return false, 0, 0, 0, 0, "", "", "", newErr
		} else {
			if qldOutlets[0].ProtocolId == 10 || qldOutlets[0].ProtocolId == 8 || qldOutlets[0].ProtocolId == 11 {
				return protocol.QiCheGuoBiaoClose(hostUrl, userId, snNum)
			} else if qldOutlets[0].ProtocolId == 9 || qldOutlets[0].ProtocolId == 12 {
				return protocol.ShengHongClose(hostUrl, userId, snNum)
			} else if qldOutlets[0].ProtocolId == 13 {
				return protocol.YuanYingClose(hostUrl, userId, snNum)
			} else if qldOutlets[0].ProtocolId == 16 {
				return protocol.PonovoSocketClose(hostUrl, userId, snNum)
			} else if qldOutlets[0].ProtocolId == 17 {
				return protocol.LvccProtocolClose(hostUrl, userId, snNum)
			} else {
				return protocol.QiCheGuoBiaoClose(hostUrl, userId, snNum)
				//return false, 0, 0, 0, 0, "", "", "", nil
			}
		}
	}
}
func getProtocolHostUrl(protocolId int64) (hostUrl string, err error) {
	qldOutletProtocol, err := models.GetQldOutletProtocolById(protocolId)
	if err != nil {
		newErr := utils.WrapError(err, "getProtocolHostUrl:1,获取设备协议失败,protocolId="+strconv.FormatInt(protocolId, 10))
		return "", newErr
	}
	return qldOutletProtocol.GrpcAddress + ":" + qldOutletProtocol.GrpcPort, nil
}
func DeviceForceClose(userId int64, snNum string) (timeOutFlag bool, closeFlag int64, amount int64, electricity float32, duration int64, orderSn, recordAmount, closeMsg string, err error) {
	qldOutlets, err := models.GetQldOutletsBySnNum(snNum)
	if err != nil {
		newErr := utils.WrapError(err, "DeviceClose:获取设备信息失败，"+snNum)
		return false, 0, 0, 0, 0, "", "", "", newErr
	} else if len(qldOutlets) == 0 {
		newErr := utils.NewError(utils.ErrorTypeStop, "DeviceClose:获取设备失败，"+snNum)
		utils.MyPrint(utils.PRINT_ERROR, newErr.Error())
		return false, 0, 0, 0, 0, "", "", "", newErr
	} else {
		hostUrl, err := getProtocolHostUrl(qldOutlets[0].ProtocolId)
		if err != nil {
			newErr := utils.WrapError(err, "DeviceClose:2,获取设备协议服务路径失败，"+snNum)
			return false, 0, 0, 0, 0, "", "", "", newErr
		} else {
			if qldOutlets[0].ProtocolId == 10 || qldOutlets[0].ProtocolId == 8 || qldOutlets[0].ProtocolId == 11 {
				return protocol.QiCheGuoBiaoForceClose(hostUrl, userId, snNum)
			} else if qldOutlets[0].ProtocolId == 9 || qldOutlets[0].ProtocolId == 12 {
				return protocol.ShengHongForceClose(hostUrl, userId, snNum)
			} else if qldOutlets[0].ProtocolId == 16 {
				return protocol.PonovoSocketClose(hostUrl, userId, snNum)
			} else if qldOutlets[0].ProtocolId == 17 {
				return protocol.LvccProtocolClose(hostUrl, userId, snNum)
			} else {
				return protocol.QiCheGuoBiaoForceClose(hostUrl, userId, snNum)
				//return false, 0, 0, 0, 0, "", "", "", nil
			}
		}
	}
}
